package com.bff.gaia.mix.api.xjoin2.core.utils;

import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;

public class XjoinThreadFactory implements ThreadFactory {

	private final static AtomicInteger POOL_NUMBER = new AtomicInteger(1);
	private final static AtomicInteger THREAD_NUMBER = new AtomicInteger(1);
	private final ThreadGroup group;
	private final String namePrefix;

	public XjoinThreadFactory(String factoryName) {
		SecurityManager s = System.getSecurityManager();
		this.group = (s != null) ? s.getThreadGroup() :
			Thread.currentThread().getThreadGroup();
		this.namePrefix = "xjoin-" + factoryName + "-pool-" +
			POOL_NUMBER.getAndIncrement() +
			"-thread-";
	}

	@Override
	public Thread newThread(Runnable r) {
		Thread t = new Thread(group, r,
			namePrefix + THREAD_NUMBER.getAndIncrement(),
			0);
		if (t.isDaemon()) {
			t.setDaemon(false);
		}
		if (t.getPriority() != Thread.NORM_PRIORITY) {
			t.setPriority(Thread.NORM_PRIORITY);
		}
		return t;
	}
}